/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.*;

/**
 *
 * @author ImNotPro
 */
public class ControllerWD {

    public static void WDclientMaker(ArrayList<WorkingDirectory> WD) {
        for (int i = 0; i < WD.size(); ++i) {
            try {
                ArrayList<Data> List = new ArrayList<Data>();
                ReadAllFile(WD.get(i).getDir(), WD.get(i).getPath(), List);
                WD.get(i).setStructDir(List);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ControllerWD.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void WDserverWriter(ArrayList<WorkingDirectory> WorkDir, String Repository, String username, int revision) {
        CreateingLastRevision(Repository, username, revision);
        for (int i = 0; i < WorkDir.size(); ++i) {
            String loc = Repository + "/" + username + "/" + revision + "/" + WorkDir.get(i).getDir();
            deleteDirectory(new File(loc));
            WriteListDatainServer(Repository, username, revision, WorkDir.get(i).getStructDir());
        }
    }

    public static ArrayList<WorkingDirectory> WDserverMaker(String Repository, String username, int Revision) {
        ArrayList<WorkingDirectory> SWD = new ArrayList<WorkingDirectory>();
        String loc = Repository + "/" + username + "/" + Revision;
        File dir = new File(loc);
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            for (int i = 0; i < children.length; i++) {
                File file = new File(loc + "/" + children[i]);
                if (!file.isHidden()) {
                    try {
                        WorkingDirectory tempWD = new WorkingDirectory();
                        tempWD.setDir(children[i]);
                        ArrayList<Data> list = new ArrayList<Data>();
                        ReadAllFile(children[i], loc, list);
                        tempWD.setStructDir(list);
                        SWD.add(tempWD);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(ControllerWD.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        return SWD;
    }

    public static void WDclientWriter(ArrayList<WorkingDirectory> ClientWD, ArrayList<WorkingDirectory> ServerWD) {
        for (int i = 0; i < ClientWD.size(); ++i) {
            for (int j = 0; j < ServerWD.size(); ++j) {
                if (ClientWD.get(i).getDir().equals(ServerWD.get(j).getDir())) {
                    String loc = ClientWD.get(i).getPath() + "/" + ClientWD.get(i).getDir();
                    deleteDirectory(new File(loc));
                    WriteAllFile(ServerWD.get(j).getStructDir(), ClientWD.get(i).getPath());
                    break;
                }
            }
        }
    }

    public static void WDclientWriterRevert(ArrayList<WorkingDirectory> ClientWD, ArrayList<WorkingDirectory> ServerWD) {
        for (int j = 0; j < ClientWD.size(); ++j) {
            String loc = ClientWD.get(j).getPath() + "/" + ClientWD.get(j).getDir();
            deleteDirectory(new File(loc));
        }
        for (int i = 0; i < ServerWD.size(); ++i) {
            for (int j = 0; j < ClientWD.size(); ++j) {
                if (ClientWD.get(j).getDir().equals(ServerWD.get(i).getDir())) {
                    WriteAllFile(ServerWD.get(i).getStructDir(), ClientWD.get(j).getPath());
                    break;
                }
            }
        }
    }

    private static void ReadAllFile(String dirc, String path, ArrayList<Data> list) throws FileNotFoundException {
        String loc = "";
        loc = path + "/" + dirc;

        File dir = new File(loc);
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            for (int i = 0; i < children.length; i++) {
                // Get filename of file or directory
                String filename = children[i];
                File file = new File(loc + "/" + filename);
                if (!file.isHidden()) {
                    if (file.isDirectory()) {
                        ReadAllFile(dirc + "/" + file.getName(), path, list);
                    } else {
                        try {
                            Data temp = new Data();
                            temp.setName(file.getName());
                            temp.setLoc(dirc);
                            int byteSize = (int) file.length();
                            byte[] b = new byte[byteSize];
                            FileInputStream fileInputStream = new FileInputStream(file);
                            fileInputStream.read(b);
                            fileInputStream.close();
                            temp.setByte(b);
                            list.add(temp);
                        } catch (IOException ex) {
                            Logger.getLogger(ControllerWD.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
    }

    private static void WriteListDatainServer(String Repository, String username, int Revision, ArrayList<Data> data) {
        String loc = Repository + "/" + username + "/" + Revision;
        WriteAllFile(data, loc);
    }

    private static void WriteListDatainClient(String path, ArrayList<Data> data) {
        WriteAllFile(data, path);
    }

    private static void CreateingLastRevision(String Repository, String username, int Revision) {
        if (Revision > 1) {
            ArrayList<WorkingDirectory> LastWD = WDserverMaker(Repository, username, (Revision - 1));
            for (int i = 0; i < LastWD.size(); ++i) {
                WriteListDatainServer(Repository, username, Revision, LastWD.get(i).getStructDir());
            }
        }
    }

    private static void WriteAllFile(ArrayList<Data> data, String path) {
        for (int i = 0; i < data.size(); ++i) {
            try {
                FileOutputStream fos = null;
                Data temp = data.get(i);
                File dir = new File(path + "/" + temp.getLoc());
                File file = new File(path + "/" + temp.getLoc() + "/" + temp.getName());
                dir.mkdirs();
                file.createNewFile();
                fos = new FileOutputStream(file);
                fos.write(temp.getByte());
                fos.flush();
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(ControllerWD.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private static boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }
}
